// pages/coursehourse/index.js
var app=getApp();
function getRandomColor(){
  let rgb=[];
  for(let i=0;i<3;++i){
    let color=Math.floor(Math.random() * 256).toString(16);
    color=color.length==1 ? '0'+color:color;
    rgb.push(color);
  }
  return '#'+rgb.join('');
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
     course:null,
     coursehour:null,
     src:'',
     elem:'myVideo',
     teacherInfo:null,
     video:false,
     content:'',
     danmuList:[
       {
        text:'第1s出现的弹幕',
        color:'#ff0000',
        time:1
       }
     ],
    studybtonShow:false,
    pauseTime:0,
  },
  bindInputBlur:function(e){
    this.inputValue=e.detail.value;
  },
  
  
  bindSendDanmu:function(){
    this.videoContext.sendDanmu({
      text:this.inputValue,
      color:getRandomColor()
    })
  },
  videoErrorCallback:function(e){
    console.log('视频错误信息：');
    console.log(e.detail.errMsg);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var _lockstate = options.lock;
    if(_lockstate=="false"){
      _lockstate = true;
    }
    this.setData({
      course:options,
      lock:_lockstate
    });
    var studyId = options.studyId;
    var pauseTime = wx.getStorageSync(studyId + 'pauseTime');
    this.setData({
      pauseTime: pauseTime,
    })
    var ok = options.difference;
    if (ok == 'ok') {
      this.setData({
        studybtonShow: true
      })
    }
      var _this = this;

    app.paiRequest("/app/course/tcourses/courseHours", options).then(function (res) {
      var data = res.data;
      var remarks = data.remarks;
      var video = data.video;
      console.log(remarks);
      wx.setNavigationBarTitle({
        title: data.courseTitle,
      })
      _this.setData({
        teacherInfo: data.appUser,
        coursehour: data
      })
      if (video){
        _this.setData({
          video: video
        })
      }

      if (remarks != undefined && remarks.length > 0) {

        _this.setData({
          src: remarks,
        })
        
      } else {
        wx.showToast({
          title: "资源获取失败，请购买后观看",
          icon: 'none',
          duration: 3000,
          mask: true
        })
        remarks = "";
      }
    })
     
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.videoContext=wx.createVideoContext("myVideo");
  },
  inputValue:'',

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var userInfo = app.globalData.userInfo;
    if(userInfo==null){
      wx.navigateTo({
        url: '/pages/login/index',
      })
    }
    

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.savePauseTime();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.savePauseTime();
  },
  savePauseTime:function(){
    var difference = this.data.course.difference;
    var studyId = this.data.course.studyId;
    if (difference != 'ok') {
      app.paiRequest("/app/course/study/tcourseStudy/pauseCourse", {
        id: studyId,
        pauseTime: this.data.currentTime
      }).then(function (res) {
        console.log(res)
      })
    }
    wx.setStorage({
      key: studyId + 'pauseTime',
      data: this.data.currentTime,
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
 
  courseNav:function(e){

    var coursehourseId = e.currentTarget.dataset.id;
    var courseHourseTitle = e.currentTarget.dataset.title;
    var coursetitle = this.data.course.courseTitle;
    var src=e.currentTarget.dataset.src;
    var questionType = e.currentTarget.dataset.questiontype
    //得到下一题的题目ID，是解锁的必要参数
    var nextCourseHoursId = this.data.course.nextCourseHourseId;



    src = src+"?parentCode=" + coursehourseId
        + "&coursetitle=" + coursetitle
        + "&questionPageSource=" +questionType
        ;
    var coursehour = this.data.coursehour;
    var sumitdata={
      courseId: coursehour.parentCode,
      courseHourseId: coursehourseId,
      courseHourseTitle: courseHourseTitle,
      courseTitle: coursetitle,
      remarks: this.data.course.courseContent,
    };
    app.paiRequest("/app/course/exercise/user/save", sumitdata).then(function (res) {

      var data=res.data;
      src = src + "&exercisesId=" + data.id + "&courseId=" + coursehour.parentCode
          + "&courseHourseId=" + coursehourseId+
          "&nextCourseHoursId="+nextCourseHoursId;
      wx.navigateTo({
        url: src,
      })
    })
   
  },
  questionpage:function(e){
    console.log(e);
    var src=e.currentTarget.dataset.src;
    var course=this.data.course;
    var coursehour = this.data.coursehour;
    wx.navigateTo({
      url: src + '?courseId=' + coursehour.parentCode + '&courseName=' + course.courseTitle + '&courseHourId=' + coursehour.id + '&courseHourName=' + coursehour.courseTitle,
    })
  },
  metadata:function(e){
    var duration=e.detail.duration;
    var studyId = this.data.course.studyId;
    var pauseTime=wx.getStorageSync(studyId + 'pauseTime');
    if (pauseTime >= duration){
      pauseTime=0;
    }
     this.setData({
       duration: e.detail.duration,
       pauseTime: pauseTime,
     })
  },
  courseEnded:function(e){
    var difference = this.data.course.difference;
    var _this=this;
    if (difference!='ok'){
    app.paiRequest("/app/course/study/tcourseStudy/endedCourse", { 
      id: this.data.course.studyId,
      duration: this.data.duration
     }).then(function (res) {
       var ok = res.data.difference;
       if(ok=='ok'){
         _this.setData({
           studybtonShow: true
         })
       }
    })
    }
    
  },
  pauseCourse:function(e){    
    var difference = this.data.course.difference;
    if (difference != 'ok') {
      app.paiRequest("/app/course/study/tcourseStudy/pauseCourse", { 
        id: this.data.course.studyId,
        pauseTime: this.data.currentTime
        }).then(function (res) {
        console.log(res)
      })
    }
  },
  studyCourse:function(e){
    var difference = this.data.course.difference;
    if (difference != 'ok') {    
      app.paiRequest("/app/course/study/tcourseStudy/playCourse", { id: this.data.course.studyId}).then(function (res) {
        
      })
    }
  },
  timeupdate:function(e){
    this.setData({
      currentTime: e.detail.currentTime
    })
  }
})